package com.wink_172.library.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.wink_172.library.R;
import com.wink_172.library.utils.DisplayUtil;

/**
 *
 * 圆角图片 可搭配xutils
 * 自定义圆角大小
 * 使用方法
 *
 *   <com.wink_172.library.view.RoundImageViewXutils
 *                 android:id="@+id/picture_view"
 *                 android:layout_width="140dp"
 *                 android:layout_height="140dp"
 *                 android:background="@null"
 *                 android:onClick="@{onClickListener}"
 *                 app:corners_size="10" />
 *
 *
 * 两者二选一  长度不一样
 *     app:corners_size="10"
 *     app:corners_size_dp="10dp"
 * */
public class RoundImageViewXutils extends ImageView {

    float corners=0.0f;//圆角大小
    float corners_dp= 0;//圆角大小

    public RoundImageViewXutils(Context context) {
        super(context);
    }

    public RoundImageViewXutils(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.RoundImageViewXutils, 0, 0);

        corners = a.getFloat(R.styleable.RoundImageViewXutils_corners_size, 0.0f);
        corners_dp=a.getDimension(R.styleable.RoundImageViewXutils_corners_size_dp,
                DisplayUtil.dp2px(getContext(),0));
        a.recycle();

    }

    public RoundImageViewXutils(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public RoundImageViewXutils(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Path clipPath = new Path();
        int w = this.getWidth();
        int h = this.getHeight();

        if(corners<=0){
            clipPath.addRoundRect(new RectF(0, 0, w, h), corners_dp, corners_dp, Path.Direction.CW);
        }else {
            clipPath.addRoundRect(new RectF(0, 0, w, h), corners, corners, Path.Direction.CW);
        }

        canvas.clipPath(clipPath);
        super.onDraw(canvas);
    }
}
